#include "imageenhancedialog.h"
#include "imageprocessor.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QFormLayout>

ImageEnhanceDialog::ImageEnhanceDialog(const QImage& originalImage, QWidget *parent)
    : QDialog(parent)
    , m_originalImage(originalImage)
{
    m_imageProcessor = new ImageProcessor(this);
    setupUI();
    createConnections();
    updatePreview();
}

void ImageEnhanceDialog::setupUI()
{
    setWindowTitle(tr("图像增强"));
    
    // 创建预览区域
    m_previewView = new ZoomablePreview(this);
    
    // 创建控制区域
    QGroupBox *controlGroup = new QGroupBox(tr("参数调整"), this);
    QFormLayout *formLayout = new QFormLayout;
    
    // 亮度滑块
    m_brightnessSlider = new QSlider(Qt::Horizontal);
    m_brightnessSlider->setRange(-100, 100);
    m_brightnessSlider->setValue(0);
    formLayout->addRow(tr("亮度:"), m_brightnessSlider);
    
    // 对比度滑块
    m_contrastSlider = new QSlider(Qt::Horizontal);
    m_contrastSlider->setRange(-100, 100);
    m_contrastSlider->setValue(0);
    formLayout->addRow(tr("对比度:"), m_contrastSlider);
    
    // 锐化滑块
    m_sharpnessSlider = new QSlider(Qt::Horizontal);
    m_sharpnessSlider->setRange(0, 100);
    m_sharpnessSlider->setValue(0);
    formLayout->addRow(tr("锐化:"), m_sharpnessSlider);
    
    // 降噪复选框
    m_denoiseCheck = new QCheckBox(tr("降噪"));
    formLayout->addRow("", m_denoiseCheck);
    
    // 自动纠偏复选框
    m_autoRotateCheck = new QCheckBox(tr("自动校正"));
    formLayout->addRow("", m_autoRotateCheck);
    
    // 颜色模式下拉框
    m_colorModeCombo = new QComboBox;
    m_colorModeCombo->addItems({tr("原色"), tr("灰度"), tr("黑白"), tr("深褐色"), tr("反色")});
    formLayout->addRow(tr("颜色模式:"), m_colorModeCombo);
    
    // 预设方案下拉框
    m_presetCombo = new QComboBox;
    m_presetCombo->addItems({tr("自定义"), tr("文本阅读"), tr("漫画模式"), tr("高对比度")});
    formLayout->addRow(tr("预设方案:"), m_presetCombo);
    
    controlGroup->setLayout(formLayout);
    
    // 创建按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    m_resetButton = new QPushButton(tr("重置"), this);
    m_applyButton = new QPushButton(tr("应用到当前页"), this);
    m_applyAllButton = new QPushButton(tr("应用到所有页"), this);
    m_cancelButton = new QPushButton(tr("取消"), this);
    
    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(m_applyButton);
    buttonLayout->addWidget(m_applyAllButton);
    buttonLayout->addWidget(m_cancelButton);
    
    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(m_previewView);
    mainLayout->addWidget(controlGroup);
    mainLayout->addLayout(buttonLayout);
    
    setLayout(mainLayout);
}

void ImageEnhanceDialog::createConnections()
{
    // 参数变化时更新预览
    connect(m_brightnessSlider, &QSlider::valueChanged, this, &ImageEnhanceDialog::updatePreview);
    connect(m_contrastSlider, &QSlider::valueChanged, this, &ImageEnhanceDialog::updatePreview);
    connect(m_sharpnessSlider, &QSlider::valueChanged, this, &ImageEnhanceDialog::updatePreview);
    connect(m_denoiseCheck, &QCheckBox::toggled, this, &ImageEnhanceDialog::updatePreview);
    connect(m_autoRotateCheck, &QCheckBox::toggled, this, &ImageEnhanceDialog::updatePreview);
    connect(m_colorModeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), 
            this, &ImageEnhanceDialog::updatePreview);
    
    // 预设方案切换
    connect(m_presetCombo, &QComboBox::currentTextChanged, this, &ImageEnhanceDialog::applyPreset);
    
    // 按钮操作
    connect(m_resetButton, &QPushButton::clicked, this, &ImageEnhanceDialog::resetParameters);
    connect(m_applyButton, &QPushButton::clicked, [this]() {
        m_clickedButton = m_applyButton;
        accept();
    });
    connect(m_applyAllButton, &QPushButton::clicked, [this]() {
        m_clickedButton = m_applyAllButton;
        accept();
    });
    connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
}

void ImageEnhanceDialog::updatePreview()
{
    EnhanceParams params;
    params.brightness = m_brightnessSlider->value();
    params.contrast = m_contrastSlider->value();
    params.sharpness = m_sharpnessSlider->value();
    params.denoise = m_denoiseCheck->isChecked();
    params.autoRotate = m_autoRotateCheck->isChecked();
    params.colorMode = m_colorModeCombo->currentIndex();
    
    // 使用 m_imageProcessor 处理图像
    m_previewImage = m_imageProcessor->enhanceImage(m_originalImage, params);
    m_previewView->updateImageKeepView(m_previewImage);
}

void ImageEnhanceDialog::resetParameters()
{
    m_brightnessSlider->setValue(0);
    m_contrastSlider->setValue(0);
    m_sharpnessSlider->setValue(0);
    m_denoiseCheck->setChecked(false);
    m_autoRotateCheck->setChecked(false);
    m_colorModeCombo->setCurrentIndex(0);
    m_presetCombo->setCurrentIndex(0);
}

void ImageEnhanceDialog::applyPreset(const QString& presetName)
{
    if (presetName == tr("文本阅读")) {
        m_brightnessSlider->setValue(10);
        m_contrastSlider->setValue(20);
        m_sharpnessSlider->setValue(30);
        m_denoiseCheck->setChecked(true);
        m_autoRotateCheck->setChecked(true);
        m_colorModeCombo->setCurrentIndex(1);  // 灰度
    }
    else if (presetName == tr("漫画模式")) {
        m_brightnessSlider->setValue(0);
        m_contrastSlider->setValue(30);
        m_sharpnessSlider->setValue(20);
        m_denoiseCheck->setChecked(true);
        m_autoRotateCheck->setChecked(false);
        m_colorModeCombo->setCurrentIndex(2);  // 黑白
    }
    else if (presetName == tr("高对比度")) {
        m_brightnessSlider->setValue(10);
        m_contrastSlider->setValue(50);
        m_sharpnessSlider->setValue(40);
        m_denoiseCheck->setChecked(false);
        m_autoRotateCheck->setChecked(false);
        m_colorModeCombo->setCurrentIndex(0);  // 原色
    }
}

EnhanceParams ImageEnhanceDialog::getParameters() const
{
    EnhanceParams params;
    params.brightness = m_brightnessSlider->value();
    params.contrast = m_contrastSlider->value();
    params.sharpness = m_sharpnessSlider->value();
    params.denoise = m_denoiseCheck->isChecked();
    params.autoRotate = m_autoRotateCheck->isChecked();
    params.colorMode = m_colorModeCombo->currentIndex();
    return params;
} 